home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-07-30 | 4.6 KB | 207 lines | [TEXT/RLAB] |
- MATRIX:
-
- The MATRIX object is the fundamental data object in RLaB.
- Matrices are dynamic data structures that can be created,
- enlarged, or destroyed by the user. Matrices can be numeric,
- real or complex or string.
-
- Creating a matrix interactively is simple:
-
- m = [1,2,3;4,5,6;7,8,9];
-
- or
-
- m = [1,2,3;
- 4,5,6;
- 7,8,9]
-
- or
-
- m=[1,2,3;4,5,...
- 6;7,8,9]
-
- As you can see the syntax is similar to MATLAB, except you
- cannot leave out the commas. However, you can break the line
- at the semicolon, but ONLY at the semicolon, and the semicolon
- cannot be left out.
-
- Matrices can be composed of other scalars, vectors and
- matrices...
-
- > s = 1;
- > v = 4:6;
- > m = [s, 2, 3; v]
- m =
- matrix columns 1 thru 3
- 1 2 3
- 4 5 6
-
- To extract, or assign to matrix elements:
-
- > m[1;1]
- 1
- > m[1;1] = 100
- m =
- matrix columns 1 thru 3
- 100 2 3
- 4 5 6
-
- When assigning values to matrix elements index checking is
- automatically performed. When an index value exceeds the
- dimension of the matrix, the size of the matrix is
- automatically extended to accommodate the new value.
-
- clear(m);
- m[1;1] = 10;
-
- Then the variable is automatically converted to a matrix
- object, and sized appropriately.
-
- The following works, but is very inefficient:
-
- for(i in 1:4) {
- for(j in 1:4) {
- a[i;j] = i+j;
- }
- }
-
- since we are constantly extending the size of the matrix.
- However, it is very convenient.
-
- Vectors can be used as matrix index values:
-
- > a=[1,2,3,4;5,6,7,8;9,10,11,12]
- a =
- matrix columns 1 thru 4
- 1 2 3 4
- 5 6 7 8
- 9 10 11 12
-
- > a[2,3;2,3]
- matrix columns 1 thru 2
- 6 7
- 10 11
-
- > a[2;]
- matrix columns 1 thru 4
- 5 6 7 8
-
- > a[;2]
- matrix columns 1 thru 1
- 2
- 6
- 10
-
- > a[;2] = 200,600,1000
- a =
- matrix columns 1 thru 4
- 1 200 3 4
- 5 600 7 8
- 9 1e+03 11 12
-
- Matrix properties can be obtained from functions such as:
- class(), type(), and size(). Additionally they can be obtained
- by using the LIST-like syntax to reference the data:
-
- > m=[1,2,3;4,5,6;7,8,9;10,11,12]
- m =
- matrix columns 1 thru 3
- 1 2 3
- 4 5 6
- 7 8 9
- 10 11 12
- > m.class
- num
- > m.type
- real
- > m.nr
- 4
- > m.nc
- 3
- > m.n
- 12
- > show ( m )
- name: m
- class: num
- type: real
- nr: 4
- nc: 3
-
- The show() function shows all of the available entity
- information as once.
-
- Matrices can also be used like single dimension arrays. Any
- matrix can be indexed with a single dimension `m[i]'. The
- effect is to walk the matrix in a column fashion, thus:
-
- > m
- m =
- matrix columns 1 thru 3
- 1 2 3
- 4 5 6
- 7 8 9
- 10 11 12
- > m[1]
- 1
- > m[2]
- 4
- > m[3]
- 7
-
- Matrices can also be forced into the shape of a column vector
- with the `[:]' operator.
-
- > m[:]
- matrix columns 1 thru 1
- 1
- 4
- 7
- 10
- 2
- 5
- 8
- 11
- 3
- 6
- 9
- 12
-
- ================================================================
-
- String Matrices:
-
- Strings matrices, belong to the string class, and not the
- numeric class. String matrices have a different internal
- representation than numeric matrices. String matrices can not
- be operated on with numeric matrices, or in the same fashion.
-
- The individual members are the same as RLaB's scalar string
- object. The syntax for creating a string matrix is the same as
- a numerical matrix, except the elements are strings, and not
- numbers.
-
- > sm = [ "s-element-1,1", "s-element-1,2";
- > "s-element-2,1", "s-element-2,2"]
- sm =
- s-element-1,1 s-element-1,2
- s-element-2,1 s-element-2,2
-
- > sm[1;2]
- s-element-1,2
- > sm[3]
- s-element-1,2
-
- > show ( sm )
- name: sm
- class: string
- type:
- nr: 2
- nc: 2
-
- > show ( sm[1;1] )
- name: NULL
- class: string
- type:
- nr: 1
- nc: 1
-